//导包
import {
  getBaseUrl,
  requestUtil
} from '../../utils/requestUtil'
import regeneratorRuntime from '../../lib/runtime/runtime'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //轮播图数组
    baseUrl: '',
    allHealthProductList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const baseUrl = getBaseUrl();
    this.setData({
          baseUrl
    })
    this.getAllHealthProductList();
  },
  /**
 * 获取健康套餐推荐
 */
async getAllHealthProductList() {
  const result = await requestUtil({
    url: '/product/findAllHotHealth',
    method: "GET"
  });
   // 计算每个套餐的总价格
   const allHealthProductList = result.message.map(item => {
    const totalPrice = item.productHealthItems.reduce((total, current) => {
      return total + (current.product.price || 0); // 防止 undefined
    }, 0)*item.discount;
    return {
      ...item,
      totalPrice: totalPrice.toFixed(2) // 格式化为两位小数
    };
  });
  this.setData({
    allHealthProductList,
  })
},
})